package com.playwright.xhs.grab;

import com.microsoft.playwright.*;
import com.microsoft.playwright.options.AriaRole;

import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author liyanhao
 * @Date 2024-01-17 11:29
 */
public class 自定义录制 {
    public static void main(String[] args) throws InterruptedException {
        //自定义录制
        extracted();

    }



    private static void extracted() throws InterruptedException {
        try (Playwright playwright = Playwright.create()) {
            BrowserType.LaunchOptions launchOptions = new BrowserType.LaunchOptions();
            launchOptions.setExecutablePath(Path.of("C:\\Users\\lyh\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe"));
            launchOptions.setHeadless(false).setSlowMo(500);
            Browser browser = playwright.chromium().connectOverCDP("http://localhost:9222");
            //保存登录态
            String statePath = Paths.get("D:\\IdeaProjects\\JavaTechnologyLearn\\_8106-playwright\\src\\main\\java\\com\\playwright\\xhs\\auth\\xhs_auth.json").toString();
            BrowserContext.StorageStateOptions options = new BrowserContext.StorageStateOptions();
            options.setPath(Paths.get(statePath));
            // 加载之前保存的浏览器状态以保持登录状态
            BrowserContext context = browser.newContext(new Browser.NewContextOptions()
                    .setStorageStatePath(Paths.get(statePath)));

            Page page = context.newPage();
            page.navigate("https://www.xiaohongshu.com/explore");
//            amountDrawn(playwright, page);
//            interceptRequest(playwright, page);

            // Setup context however you like.
            context.route("**/*", route -> route.resume());
            // Pause the page, and start recording manually.

            page.pause();




        }
    }

    public static void amountDrawn(Playwright playwright, Page page){
        Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions()
                .setHeadless(false));
        BrowserContext context = browser.newContext();
        page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("报备额度")).click();
//        Locator.FilterOptions filterOptions = new Locator.FilterOptions();
//        Locator.FilterOptions 申请今日提额 = filterOptions.setHasText("申请今日提额");
//        page.locator("a").filter(申请今日提额).click();
//        page.getByText("单次可申请的最大提额量：80（与店铺发货量有关）").click();
//        page.getByTestId("beast-core-inputNumber-htmlInput").click();
//        page.getByTestId("beast-core-inputNumber-htmlInput").fill("80");
//        page.getByTestId("beast-core-textArea-htmlInput").fill("手机报备额度查看");
//        page.getByTestId("beast-core-modal-ok-button").click();
    }

    public static  void interceptRequest(Playwright playwright, Page page) throws InterruptedException {
        // 设置请求拦截器
//        page.onRequest((request) -> {
//            System.out.println("接口请求开始:");
//            System.out.println("请求 URL: " + request.url());
//            System.out.println("请求方法: " + request.method());
//            System.out.println("请求 headers: " + request.headers());
//        });
        Thread.sleep(1000);
        int length = page.getByTestId("beast-core-table").locator("div").count();
        for (int j = 0; j < length; j++) {
            Locator div = page.getByTestId("beast-core-table").locator("div").
                    nth(j).first();
            String s = div.getByRole(AriaRole.ROW).getByRole(AriaRole.CELL).nth(0).textContent();
            System.out.println(s);
            String s1 = div.getByRole(AriaRole.ROW).getByRole(AriaRole.CELL).nth(1).textContent();
            System.out.println(s1);
            String s2 = div.getByRole(AriaRole.ROW).getByRole(AriaRole.CELL).nth(2).textContent();
            System.out.println(s2);
        }





//        page.onResponse((response)->{
//            System.out.println("接口请求开始:");
//            if("https://mms.pinduoduo.com/hades/decrypt/quota/apply".equals(response.url())){
//                byte[] body = response.body();
//                try {
//                    System.out.println(new String(body, "utf-8"));
//                } catch (UnsupportedEncodingException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//        });
    }


}
